package month1;

public class ReverseLinkedList206_04 {
    public static void main(String[] args) {
        System.out.println(reverseList(new ListNode(new int[]{1, 2, 4,})));
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int[] arr) {
            ListNode tmp = this;
            for (int a : arr) {
                tmp.next = new ListNode(a);
                tmp = tmp.next;
            }
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    static ListNode reverseList(ListNode head) {
        ListNode ret = null, node = head;
        while (node != null) {
            ListNode next = node.next;
            node.next = ret;
            ret = node;
            node = next;
        }
        return ret;
    }
}
